<script type="text/javascript">
(function(){
	<%
		local para = {
			network = {lan = {}}
		}

		local result = tpl_get_data(para)
		local lan = result[K_MODULE].network.lan
	-%>

	var ip = '<%=lan.ip%>';
	var netmask = '<%=lan.netmask%>';

	$("#LanIp").val(ip);

	/* 合法性检查错误提示 */
	function errHandle(errNo)
	{
		var noteStr = "";

		switch (errNo)
		{
		case E_NONE:// 没有错误
			return false;
		case E_INVNET:// 非法网段
			noteStr = errStr.ipAddrNetErr;
			break;
		case E_INVIP:// IP地址不正确
			noteStr = errStr.ipAddrErr;
			break;
		case E_INVIPFMT:// IP地址格式错误
			noteStr = errStr.ipAddrFmtErr;
			break;
		case E_INVGROUPIP:// 组播的IP地址
			noteStr = errStr.ipAddrGroupErr;
			break;
		case E_INVLOOPIP:// 回环的IP地址
			noteStr = errStr.ipAddrLoopErr;
			break;
		case E_COMFLICTNET:// 网段冲突
			noteStr = errStr.ipAddrLanWanConflict;
			break;
		case E_INVLANIP:// 非法LAN侧IP地址
			noteStr = errStr.invLanIp;
			break;
		case E_INVARG:// 参数错误
			noteStr = errStr.argsInvalid;
			break;
		default:
			noteStr = errStr.invRequestFail;
			break;
		}

		showNote(noteStr);

		return true;
	}

	$("#Save").click(function(){
		var lanIp = $("#LanIp").val();

		if (lanIp == ip){
			showAlert(label.ipNoModify);
			return;
		}

		saveLan();
	});

	function saveLan(){
		var data = {};
		var para = {};
		para.network = {};
		para.network.lan = data;

		data.ip = $("#LanIp").val();

		if (errHandle(checkLan(data.ip, netmask))){
			return;
		}

		showLoading(label.saving);
		apiSet(para, function(ret){
			var code = ret[ERR_CODE];
			if (E_NONE == code){
				var time = ret[K_MODULE].network.lan.wait_time;
				$.setTimeout(function(){
					closeLoading();
					window.location.href = "http://" + data.ip;
				}, time * 1000);
			}else{
				$.setTimeout(function(){
					closeLoading();
					errHandle(code);
				}, 2000);
			}
		});
	}

	function checkLan(lanIp, lanMask){
		if (checkIp(lanIp) != E_NONE){
			return E_INVLANIP;
		}

		if (checkMask(lanMask) != E_NONE){
			return E_INVLANMASK;
		}

		if (checkIpMask(lanIp, lanMask) != E_NONE){
			return E_INVLANIP;
		}

		return E_NONE;
	}
})();
</script>
<div class="head-con">
	<a href="javascript:history.go(-1);" class="head-btn-l"><i class="head-btn icon icon_back"></i></a>
	<h1 class="head-title">{%label.lanSet%}</h1>
</div>
<div class="set-con below-head">
	<ul class="blank-bg"></ul>
	<ul class="set-con">
		<label class="desc-lbl">{%label.ipaddr%}</label>
		<li class="text-con">
			<input id="LanIp" class="text hover" maxLength="15" value="" />
		</li>
	</ul>
	<ul class="blank-bg"></ul>
	<div class="err-note hidden">
		<i class="err-icon icon_remind_line"></i>
		<p class="note-str"></p>
	</div>
	<div class="btn-con">
		<input id="Save" class="btn" type="button" value="{%btn.save%}" />
	</div>
</div>